<?php

define('IN_DOUCO', true);
require (dirname(__FILE__) . '/include/init.php');
// rec操作项的初始化
$rec = $check->is_rec($_REQUEST['rec']) ? $_REQUEST['rec'] : 'default';

$res = $dou->get_all("SELECT * FROM " . $dou->table('chatroom'));
foreach($res as $k=>$v) $rooms[$v['id']] = $v;

$smarty->assign('site_url', $GLOBALS['site_url']);
$smarty->assign('rooms', $rooms);
$smarty->assign('rec', $rec);
$smarty->assign('cur', 'robot');
$smarty->assign('ur_here', $_LANG['robot']);
$smarty->assign('userlist_link', array ('text' => $_LANG['robot'],'href' => 'robot.php')); 
$smarty->assign('useradd_link', array ('text' => $_LANG['robot_add'],'href' => 'robot.php?rec=add')); 

//列表
if ($rec == 'default') {
    $nickname = i('nickname','','request');
    $room_id  = i('room_id','','request');
    $mobile   = i('mobile','','request');
    $group_id = i('group_id','','request');
    $isgag    = i('isgag','','request');
    $status   = i('status','','request');
    $where = " WHERE is_robot='1' ";
    if($nickname){
        $where .= " AND nickname LIKE '%$nickname%' "; $get = '&nickname=' . $nickname;
    } 
    if($room_id != ''){
        $where .= " AND room_id='$room_id' "; $get = '&room_id=' . $room_id;
    }  
    if($group_id != ''){
        $where .= " AND group_id='$group_id' "; $get = '&group_id=' . $group_id;
    } 
    //分页
    $page = $check->is_number($_REQUEST['page']) ? $_REQUEST['page'] : 1;
    $page_url = 'robot.php?1=1';
    $limit = $dou->pager('user', 25, $page, $page_url, $where, $get);
    $sql = "SELECT * FROM " . $dou->table('user') . " $where ORDER BY id DESC $limit";
    $res = $dou->get_all($sql);
    foreach ($res as $k=>$row) {
        $user_list[$k] = $row;
        $user_list[$k]['logintime'] = date("Y-m-d H:i:s", $row['logintime']);
    }
    // 赋值给模板
    $smarty->assign('user_list', $user_list);
    $smarty->display('robot.htm');
} 

//添加
elseif ($rec == 'add') {
    $smarty->assign('ur_here', $_LANG['robot']);
    $smarty->assign('action_link', array ('text' => $_LANG['robot'],'href' => 'robot.php' ));

    // CSRF防御令牌生成
    $smarty->assign('token', $firewall->get_token());
    $smarty->assign('user_info', $user_info);
    $smarty->display('robot.htm');
} 
elseif ($rec == 'insert') {
    check_field();
    // CSRF防御令牌验证
    $firewall->check_token($_POST['token']);
    $_data = array(
        'nickname'=>$_POST['nickname'], 
        'username'=>$_POST['nickname'], 
        'room_id'=>$_POST['room_id'], 
        'group_id'=>$_POST['group_id'], 
        'status'=>$_POST['status'], 
        'up_week'=>@implode(',', $_POST['up_week']),
        'up_time'=>$_POST['up_time'], 
        'is_robot'=>1,
        'down_time'=>$_POST['down_time'] );
    //上传头像
    $avatar = upload_image('avatar','avatar');
    if(!empty($avatar)) $_data['avatar'] = $avatar;
    $sql = $dou->insert_sql('user', $_data);
    $dou->query($sql);
    //操作记录
    $log_msg = $_LANG['user_add'].': '.$_POST['nickname'];
    $dou->create_admin_log($log_msg);
    //更新在线用户信息
    updateUser($_POST['id']);
    $dou->dou_msg($_LANG['edit_succes'], 'robot.php');
} 

//编辑
elseif ($rec == 'edit') {
    $smarty->assign('ur_here', $_LANG['robot']);
    $smarty->assign('action_link', array ('text' => $_LANG['robot'],'href' => 'robot.php' ));
    // 验证并获取合法的ID
    $id = $check->is_number($_REQUEST['id']) ? $_REQUEST['id'] : '';
    $user_info = $dou->get_row("SELECT * FROM ".$dou->table('user')." WHERE id='$id' ");
    $user_info['up_week'] = explode(',', $user_info['up_week']);
    // CSRF防御令牌生成
    $smarty->assign('token', $firewall->get_token());
    $smarty->assign('user_info', $user_info);
    $smarty->display('robot.htm');
} 
elseif ($rec == 'update') {
    check_field();
    // CSRF防御令牌验证
    $firewall->check_token($_POST['token']);
    $_data = array(
        'nickname'=>$_POST['nickname'], 
        'room_id'=>$_POST['room_id'], 
        'group_id'=>$_POST['group_id'], 
        'status'=>$_POST['status'], 
        'up_week'=>@implode(',', $_POST['up_week']),
        'up_time'=>$_POST['up_time'], 
        'down_time'=>$_POST['down_time'] );
    //上传头像
    $avatar = upload_image('avatar','avatar');
    if(!empty($avatar)) $_data['avatar'] = $avatar;

    $sql = $dou->update_sql('user', $_data, "id='{$_POST['id']}'");
    $dou->query($sql);
    //操作记录
    $log_msg = $_LANG['user_edit'].': '.$_POST['username'];
    $dou->create_admin_log($log_msg);
    //更新在线用户信息
    updateUser($_POST['id']);
    $dou->dou_msg($_LANG['edit_succes'], 'robot.php');
} 

//删除
elseif ($rec == 'del') {
    // 验证并获取合法的ID
    $id = $check->is_number($_REQUEST['id']) ? $_REQUEST['id'] : $dou->dou_msg($_LANG['illegal'], 'robot.php');
    $nickname = $dou->get_one("SELECT nickname FROM " . $dou->table('user') . " WHERE id = '$id'");
    if (isset($_POST['confirm']) ? $_POST['confirm'] : '') {
        $dou->create_admin_log($_LANG['user_del'] . ': ' . $nickname);
        //删除在线用户列表列表
        delUser($id);
        $dou->delete($dou->table('user'), "id = $id", 'robot.php');
    } else {
        $_LANG['del_check'] = preg_replace('/d%/Ums', $nickname, $_LANG['del_check']);
        $dou->dou_msg($_LANG['del_check'], 'robot.php', '', '30', "robot.php?rec=del&id=$id");
    }
} 


function check_field(){
    global $dou,$_LANG;
    if(empty($_POST['nickname'])) $dou->dou_msg($_LANG['nickname_empty']);
    if(empty($_POST['room_id'])) $dou->dou_msg($_LANG['room_id_empty']);
    if(empty($_POST['up_time']))   $dou->dou_msg($_LANG['up_time_empty']);
    if(empty($_POST['down_time'])) $dou->dou_msg($_LANG['down_time_empty']);
    return true;
}